package lib

import (
	"crypto/md5"
	"encoding/base64"
	"encoding/hex"
	"fmt"
	"log"
	"os"
	"strconv"
	"time"

	beego "github.com/beego/beego/v2/server/web"
	//"github.com/garyburd/redigo/redis"
	//"github.com/go-redis/redis/v9"
	"github.com/lifei6671/gorand"
)

// var redispool *redis.Pool
var ProxyAddress, _ = beego.AppConfig.String("ProxyAddress")
var redisDb, _ = beego.AppConfig.String("redisDb")

// func init() {
// 	redispool = &redis.Pool{
// 		MaxIdle:     16,
// 		MaxActive:   0,
// 		IdleTimeout: 300,
// 		Dial: func() (redis.Conn, error) {
// 			return redis.Dial("tcp", redisDb)
// 		},
// 	}
// }

//create md5 string
func Strtomd5(s string) string {
	h := md5.New()
	h.Write([]byte(s))
	rs := hex.EncodeToString(h.Sum(nil))
	return rs
}

//password hash function
func Pwdhash(str string) string {
	return Strtomd5(str)
}

func StringsToJson(str string) string {
	rs := []rune(str)
	jsons := ""
	for _, r := range rs {
		rint := int(r)
		if rint < 128 {
			jsons += string(r)
		} else {
			jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
		}
	}

	return jsons
}

//判断数组中是否存在某个元素
func IsExistInArry(arry []int64, obj int64) bool {
	var est bool
	for _, bb := range arry {
		if obj == bb {
			est = true
			break
		}
	}
	return est
}

func PrintLog(filepath, logcontent string) {
	var logtime = time.Now().Format("20060102")
	var logFileName = filepath + "." + logtime
	//runtime.GOMAXPROCS(runtime.NumCPU())
	logFile, err := os.OpenFile(logFileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
	defer logFile.Close()
	if err != nil {
		log.Println("[error] can't write log!")
		os.Exit(1)
	}
	gLogger := log.New(logFile, "", 0)
	gLogger.Printf("%v", logcontent)
}

func SecurityLog(ip, user, event, content string) {
	logpath, _ := beego.AppConfig.String("securityLog")
	if logpath != "" {
		tms := time.Now().Format("2006-01-02T15:04:05")
		logtxt := fmt.Sprintf("%s | %s | %s | %s | %s", tms, ip, user, event, content)
		PrintLog(logpath, logtxt)
	}
}

func GetIpLog(content string) {
	logpath, _ := beego.AppConfig.String("security::getiplog")
	PrintLog(logpath, content)
}

//随机数字字符串
func GetRandomNumber(count uint) string {
	return gorand.RandomStringSpec1(count, "1234567890")
}

//随机字符字符串
func GetRandomString(count uint) string {
	return gorand.RandomStringSpec1(count, "1234567890abcdefghigklmnopqrstuvwxyzpowerbywangbikang")
}

func ShowDbInfo() {
	encodedStr := "6L+Z5aWX6L2v5Lu25Y+K5Luj56CB55qE54mI5p2D5b2S5rGq56Kn5bq35omA5pyJ77yM5YWB6K645L+u5pS55bm255So5LqO5YWs5Y+45oiW5Liq5Lq66Ieq55So77yM5LiN6IO95Z+65LqO6K+l5Luj56CB55So5LqO5LqM5qyh5ZSu5Y2W77yM5omA5pyJ5Z+65LqO6K+l5Luj56CB6L+b6KGM5pu05pS55bm26L+b6KGM5ZSu5Y2W55qE6KGM5Li65Z2H5Li66L+d5rOV6KGM5Li677yM5rGq56Kn5bq35Lqr5pyJ5L+d55WZ6L+956m25YW25rOV5b6L6LSj5Lu755qE5p2D5Yip44CC"
	decodedBytes, err := base64.StdEncoding.DecodeString(encodedStr)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	decodedStr := string(decodedBytes)
	fmt.Println(decodedStr)
}

func GetConfigString(key string) string {
	vv, _ := beego.AppConfig.String(key)
	return vv
}

func GetConfigInt(key string) int {
	value, _ := beego.AppConfig.Int(key)
	return value
}
